package ar.com.ford.it.fullEconomics.view.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.AgreementFacade;
import ar.com.ford.it.fullEconomics.facade.RequestFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr01Request;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr03Agreement;
import ar.com.ford.it.fullEconomics.view.form.CommentsForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class CommentsAction extends AbstractBaseAction {

    /**
     * Inicializa la pagina
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {

        CommentsForm form = (CommentsForm) actionForm;
        form.resetForm();
        Teesr01Request req = (Teesr01Request) request
                .getAttribute("requestSelected");
        Teesr03Agreement agreement = (Teesr03Agreement) request
                .getAttribute("agreementSelected");
        form.setAgreement(agreement);
        form.setRequest(req);
        if (req != null) {
            form.setComments(req.getEesr01RequestCommentsX());
        }
        if (agreement != null) {
            form.setComments(agreement.getEesr03AgreeCommentsX());
        }

        return mapping.findForward("success");
    }

    /**
     * Guarda los commentarios con el facade correspondiente
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward saveComments(FordActionMapping mapping,
                                      ActionForm actionForm,
                                      HttpServletRequest request,
                                      HttpServletResponse response,
                                      WebSession webSession,
                                      ActionMessages msgs,
                                      ActionMessages warns,
                                      ActionMessages errorMsgs) {

        ActionMessages am = new ActionMessages();
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CommentsForm form = (CommentsForm) actionForm;
        //si entro por request guardo en reuqest
        if (form.getRequest() != null) {
            RequestFacade facade = (RequestFacade) FacadeProvider.getFacade(
                    request, RequestFacade.class);
            Teesr01Request req = form.getRequest();
            req.setEesr01RequestCommentsX(form.getComments());
            try {
                facade.updateRequest(req, user);
            } catch (FullEconomicsException e) {
                am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                        .getMessageResource()));
                saveErrors(request, am);
            }
        }
        //si entro por agreement gaurdo en agreement
        if (form.getAgreement() != null) {
            AgreementFacade facade = (AgreementFacade) FacadeProvider
                    .getFacade(request, AgreementFacade.class);
            Teesr03Agreement agreement = form.getAgreement();
            agreement.setEesr03AgreeCommentsX(form.getComments());
            try {
                facade.updateAgreement(agreement, user);
            } catch (FullEconomicsException e) {
                am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                        .getMessageResource()));
                saveErrors(request, am);
            }
        }
        

        return mapping.findForward("success");
    }

}
